<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Config_model extends CI_Model {

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
        
        $this->load->database();
    
    }
	
	 /**
     * Get setting by id
     *
     * @access public
     * @param string $account_id
     * @return object account object
     */
	function get_by_id($setting_id)
    {
        return $this->db->get_where('config', array('id' => $setting_id))->row();
    }
	
	 /**
     * Get setting by name
     *
     * @access public
     * @param string $username
     * @return object account object
     */
    function get_by_name($name)
    {
        return $this->db->get_where('config', array('name' => $name))->row();
    }
	
	/* get a value by id 
	 * returns just the value!
	 */
	function value_by_id($setting_id)
    {
        $row = $this->db->get_where('pool_config', array('setting_uid' => $setting_id))->row_array();
		return $row['value'];
    }
	
	/* get a value by setting name
	 * return just the value!
	 */ 
	function item($name)
	{
	    $row = $this->db->get_where('config', array('name' => $name))->row_array();
        if(isset($row['value']))
        {
            return $row['value'];
        }
        else
        {
            return FALSE;
        }    
    }
	
	/* get all settings as an array */
	
	function get_all()
	{
      $result = $this->db->get('config')->result_array();
      return $result;
	}

  function update($name, $new_value)
  {
    if ($this->get_by_name($name))
      {
          $this->db->where('name', $name);
          $this->db->update('config', array('value' => $new_value));
      }
      // Insert
      else
      {
          $this->db->insert('config',array('value' => $new_value, 'name' => $name));
      }
  }

	
}